package com.xiaoxie.boot.controller;

import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xiaoxie.boot.bean.Role;
import com.xiaoxie.boot.service.RoleService;
import com.xiaoxie.boot.utils.DataInfo;
import com.xiaoxie.boot.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * @author xiaoxie
 * @create 2022-03-27 22:55
 */
@Api(tags = "角色API")
@RestController
@Slf4j
@RequestMapping("/role")
public class RoleController {

    @Autowired
    RoleService roleService;

    /**
     * 查询所有角色信息
     *
     * @param role
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/queryAllRole")
    @ApiOperation(value = "分页查询", notes = "查询所有角色信息")
    @ApiOperationSupport(author = "小谢")
    public DataInfo queryAllRole(Role role, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "15") Integer limit) {
        PageInfo<Role> rolePageInfo = roleService.selectAllRole(role, page, limit);
        return DataInfo.ok("成功", rolePageInfo.getTotal(), rolePageInfo.getList());
    }

    /**
     * 角色添加
     *
     * @param role
     * @param request
     * @return
     */
    @PostMapping("/addRole")
    @ApiOperation(value = "新增", notes = "角色添加")
    @ApiOperationSupport(author = "小谢")
    public DataInfo addRole(@RequestBody Role role, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute("name");
        role.setCreator(name);
        role.setCreateTime(DateUtil.getTimestamp());
        log.info(name + "添加了角色：" + role.getName());
        int i = roleService.insertSelective(role);
        if (i == 1) {
            log.info("添加成功");
            return DataInfo.ok();
        } else {
            log.info("添加失败");
            return DataInfo.fail("角色添加失败!");
        }
    }

    /**
     * 修改角色信息
     *
     * @param role
     * @param request
     * @return
     */
    @PutMapping("/editRole")
    @ApiOperation(value = "修改", notes = "修改角色信息")
    @ApiOperationSupport(author = "小谢")
    public DataInfo editRole(@RequestBody Role role, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute("name");
        log.info(name + "修改了角色：" + role.getName());
        int i = roleService.updateByPrimaryKeySelective(role);
        if (i == 1) {
            log.info("修改成功");
            return DataInfo.ok();
        } else {
            log.info("修改失败");
            return DataInfo.fail("角色修改失败!");
        }
    }

    /**
     * 删除角色信息
     *
     * @param ids
     * @param request
     * @return
     */
    @DeleteMapping("/deleteRole")
    @ApiOperation(value = "删除", notes = "删除角色信息")
    @ApiOperationSupport(author = "小谢")
    public DataInfo deleteRole(String ids, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute("name");
        List<String> list = Arrays.asList(ids.split(","));
        log.info(name + "试图删除角色信息");
        int i = roleService.deleteRoleByIds(list);
        if (i >= 1) {
            log.info("删除了：" + i + "条信息");
            return DataInfo.ok();
        } else {
            log.info("删除失败");
            return DataInfo.fail("删除失败");
        }
    }

    /**
     * 添加角色时检查角色名是否存在
     *
     * @param name
     * @return
     */
    @GetMapping("/roleExist/{name}")
    @ApiOperation(value = "去重", notes = "添加角色时检查角色名是否存在")
    @ApiOperationSupport(author = "小谢")
    public DataInfo roleExist(@PathVariable("name") String name) {
        Role role = roleService.selectRoleName(name);
        if (role == null) {
            return DataInfo.ok();
        } else {
            return DataInfo.fail("角色名已存在!");
        }
    }
}
